package common

import "github.com/gin-gonic/gin"

type Api struct {
}

type R struct {
	Code int
	Msg  string
	Data any
}

func (a *Api) Success(c *gin.Context, result *R) {
	if result.Msg == "" {
		result.Msg = "success"
	}
	if result.Code == 0 {
		result.Code = 1
	}

	a.Result(c, result.Code, result.Msg, result.Data)
}

func (a *Api) Error(c *gin.Context, result *R) {
	if result.Msg == "" {
		result.Msg = "操作失败"
	}
	a.Result(c, result.Code, result.Msg, result.Data)
}

func (a *Api) Result(c *gin.Context, code int, msg string, data any) {
	c.JSON(200, gin.H{
		"code": code,
		"msg":  msg,
		"data": data,
	})
}
